package cn.imu.appdevexperiment2020._2activity.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

import cn.imu.appdevexperiment2020.R;
import cn.imu.appdevexperiment2020._2activity.bean.Fruit;

/**
 * 描述:
 *
 * @author chenconghua
 * @version 1.0
 * @create 2022-11-06 01:40
 */
public class FruitRecAdapter extends RecyclerView.Adapter<FruitRecAdapter.ViewHolder> {

   private List<Fruit> mFruitList;
   static class ViewHolder extends RecyclerView.ViewHolder{
      ImageView fruitImage;
      TextView fruitName;

      public ViewHolder(@NonNull View itemView) {
         super(itemView);
         fruitImage=(ImageView) itemView.findViewById(R.id.fruit_image);
         fruitName =(TextView) itemView.findViewById(R.id.fruit_name);
      }
   }
   public FruitRecAdapter(List<Fruit> fruitList){
      mFruitList=fruitList;
   }
   @NonNull
   @Override
   public FruitRecAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
      View view= LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.fruit_item,viewGroup,false);
      ViewHolder holder = new ViewHolder(view);
      return holder;
   }

   @Override
   public void onBindViewHolder(@NonNull FruitRecAdapter.ViewHolder viewHolder, int i) {
      Fruit fruit = mFruitList.get(i);
      viewHolder.fruitImage.setImageResource(fruit.getImageId());
      viewHolder.fruitName.setText(fruit.getName());

   }

   @Override
   public int getItemCount() {
      return mFruitList.size();
   }
}
